using System.Linq;
using TaskKernel.Infrastructure;

namespace TaskKernel.Domain.Services.Mail
{
    public class DepartmentMailRecipient : ITaskRecipient
    {
        protected readonly RecipientList Recipients;

        public DepartmentMailRecipient(RecipientList recipients)
        {
            Recipients = recipients;
        }

        #region ITaskRecipient Members

        public virtual void AddOwner(Task task)
        {
            Recipients.Add(task.Owner.Mail);
        }

        public virtual void AddParticipants(Task task)
        {
            var participants = task.Participants.Select(x => x.Mail);
            foreach (var participant in participants)
                Recipients.Add(participant);
        }

        public virtual void AddAdmins(Task task)
        {
            var adminsInDepartments = task.Department.Admins.Select(x => x.Mail);
            foreach (var adminsInDepartment in adminsInDepartments)
                Recipients.Add(adminsInDepartment);
        }

        public virtual void AddDepartmentUsers(Task task)
        {
            var usersInDepartments = task.Department.Executors.Select(x => x.Mail);
            if (task.Executor == null){
                foreach (var usersInDepartment in usersInDepartments)
                    Recipients.Add(usersInDepartment);
            }
            else{
                if (task.Executor != null)
                    Recipients.Add(task.Executor.Mail);
            }
        }

        #endregion
    }
}